package com.ruoyi.productProof.service.impl;

import com.ruoyi.productProof.domain.ProductProof;
import com.ruoyi.productProof.mapper.ProductProofMapper;
import com.ruoyi.productProof.service.ProductProofService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

@Service
public class ProductProofServiceImpl implements ProductProofService {

    @Autowired
    private ProductProofMapper productProofMapper;

    /**
     * 查询产品注册证列表
     *
     * @param productProof 查询条件
     * @return 结果
     */
    @Override
    public List<ProductProof> selectProductProofList(ProductProof productProof) {
        return productProofMapper.selectProductProofList(productProof);
    }

    /**
     * 查询产品医疗器械注册证最新数据，不包括本条数据
     *
     * @param productProof 查询条件
     * @return 结果
     */
    @Override
    public ProductProof selectProductProof(ProductProof productProof) {
        return productProofMapper.selectProductProof(productProof);
    }

    /**
     * 插入产品注册证号
     *
     * @param productProofs 查询条件
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProductProof(List<ProductProof> productProofs) {

        //查询数据库中所拥有的产品注册证
        Long productId = productProofs.get(0).getProductId();
        ProductProof productProof1 = new ProductProof();
        productProof1.setProductId(productId);
        List<ProductProof> productProofList = productProofMapper.selectProductProofList(productProof1);

        //定义数组存储 前端传过来的产品注册证主键
        List<Long> arrayList = new ArrayList();

        //循环前端过来的数据 判断执行什么操作
        for (ProductProof productProof : productProofs) {
            if (productProof.getProductProofId() != null) {
                Long productProofId = productProof.getProductProofId();
                arrayList.add(productProofId);
                productProofMapper.updateProductProof(productProof);
            } else {
                productProofMapper.insertProductProof(productProof);
            }
        }

        //将查询出来的数据放到一个集合数组
        List<Long> longArrayList = new ArrayList<Long>();
        for (ProductProof productProof : productProofList) {
            Long productProofId = productProof.getProductProofId();
            longArrayList.add(productProofId);
        }

        //多出来的产品注册证
        TreeSet<Long> longs = new TreeSet<>();
        for (int i = arrayList.size() - 1; i >= 0; i--) {
            for (int j = longArrayList.size() - 1; j >= 0; j--) {
                if (arrayList.get(i) == longArrayList.get(j)) {
                    longArrayList.remove(j);
                }
            }
        }
        for (Long aLong : longArrayList) {
            longs.add(aLong);
        }

        //删除数组
        ArrayList<Long> longs1 = new ArrayList<>();
        if (longs.size() != 0) {
            for (Long aLong : longs) {
                longs1.add(aLong);
            }
            productProofMapper.deleteProductProof(longs1);
        }
        return 1;
    }

    /**
     * 查询需要删除的产品注册证号主键 集合
     *
     * @param productId 查询条件
     * @return 结果
     */
    @Override
    public List<Long> selectProductProofByProductId(Long productId) {
        return productProofMapper.selectProductProofByProductId(productId);
    }

    /**
     * 删除产品注册证信息
     *
     * @param productId 查询条件
     */
    @Override
    public void deleteProductProofList(List<Long> productId) {
        productProofMapper.deleteProductProof(productId);
    }

    /**
     * 查询产品注册证信息
     *
     * @param map 查询条件
     * @return 结果
     */
    @Override
    public ProductProof selectProductProofByMap(Map map) {
        ProductProof productProof = productProofMapper.selectProductProofByMap(map);
        if (productProof == null) {
            return new ProductProof();
        }
        return productProof;
    }

    /**
     * 插入产品注册证信息
     *
     * @param proof 数据
     * @return 结果
     */
    @Override
    public int insertProductProofs(ProductProof proof) {
        return productProofMapper.insertProductProof(proof);
    }

    /**
     * 修改产品注册证信息
     *
     * @param productProof 修改信息
     * @return 结果
     */
    @Override
    public int updateProductProof(ProductProof productProof) {
        return productProofMapper.updateProductProof(productProof);
    }

    /**
     * 删除产品注册证信息
     *
     * @param productProofId 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int removeProduct(Long productProofId) {
        List<Long> objects = new ArrayList<>();
        objects.add(productProofId);
        return productProofMapper.deleteProductProof(objects);
    }

    /**
     * 根据 产品id 查询该产品的注册证信息
     *
     * @param productId
     * @return
     */
    @Override
    public List<ProductProof> selectByProductId(Long productId) {
        return productProofMapper.selectByProductId(productId);
    }

    /**
     * 查询产品的医疗器械注册证信息
     *
     * @param productProof 查询条件
     * @return 结果
     */
    @Override
    public List<ProductProof> selectProductProofChange(ProductProof productProof) {
        return productProofMapper.selectProductProofChange(productProof);
    }

    @Override
    public List<ProductProof> selectProductProofListes(List<Map> saleGoodsModelMaps) {
        return productProofMapper.selectProductProofListes(saleGoodsModelMaps);
    }
}
